package com.example.controller.collar;

import com.example.pojo.PetTemperatureLimit;
import com.example.pojo.Railing;
import com.example.response.ApiResult;
import com.example.response.ErrorStatus;
import com.example.service.user.UserAuthenticationService;
import com.example.vo.collar.CollarExploreData;
import com.example.service.collar.RailingService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author w
 */
@RestController
@RequestMapping("/railing")
public class RailingController {
    @Autowired
    RailingService railingService;
    @Autowired
    UserAuthenticationService userAuthenticationService;

    @PutMapping("/")
    @ApiOperation("添加电子围栏接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "用户令牌", required = true,
                    dataTypeClass = String.class, paramType = "header")
    })
    public ApiResult<CollarExploreData> putRailing(@RequestHeader("token") String token,
                                                   @RequestBody Railing railing){
        ApiResult<CollarExploreData> response = new ApiResult<>();
        try{
            userAuthenticationService.login(token);
            int insert = railingService.insert(railing);
            if (insert!=0){
                response.setSuccess(new CollarExploreData(insert));
                return response;
            }else {
                response.setError(ErrorStatus.PARAM_FORMAT_ERROR);
                return response;
            }
        }catch (Exception e){
            e.printStackTrace();
            response.setError(ErrorStatus.SERVER_ERROR,e.getMessage());
            return response;
        }
    }

    @PutMapping("/temperature/")
    @ApiOperation("设置宠物体温警告上下限")
    public ApiResult<CollarExploreData> putTemperatureLimit(@RequestHeader("token") String token, @RequestBody PetTemperatureLimit petTemperatureLimit){
        ApiResult<CollarExploreData> response = new ApiResult<>();
        try {
            userAuthenticationService.login(token);
            double limitMax = petTemperatureLimit.getLimitMax();
            double limitMin = petTemperatureLimit.getLimitMin();
            if (limitMin<0||limitMin>=limitMax||limitMax>100){
                response.setError(ErrorStatus.PARAM_FORMAT_ERROR);
                return response;
            }
            boolean b = railingService.setPetTemperatureLimit(petTemperatureLimit);
            if (b){
                response.setSuccess(null,"体温预警设置成功");
                return response;
            }else {
                response.setError(ErrorStatus.PARAM_FORMAT_ERROR);
                return response;
            }
        }catch (Exception e){
            e.printStackTrace();
            response.setError(ErrorStatus.SERVER_ERROR,e.getMessage());
            return response;
        }
    }
}
